package controllers;

import models.*;

import play.mvc.Controller;
import play.mvc.With;

import java.util.*;

@With(Secure.class)
public class Notes extends Controller {
	public static void viewNote(long eventID) {
		Event e = Event.findById(eventID);
		boolean hasNotes = false ;	
			if(e.notes.isEmpty())
				hasNotes=false;
			else{hasNotes=true;}
				
			render(e.notes,hasNotes);
	}

	public static List<Note> getNotesByEventID(long eventID) {
		Event e = Event.findById(eventID);
		return e.notes;
	}

	
	/*deleted is the boolean variable 
	 * project is the project you want to delete , found by ID 
	 * eventList is the list of events in this project
	 * event is a variable created on the spot while looping on the list
	 * event.notes.get(j) points to a certain note in the list of notes 
	 * this note gets deleted and this event saved 
	 * condition checks whether the size of the notes list is less by 1 that the original size
	 * the other conditions were added to avoid a null pointer exception */
	public static boolean deleteNote(long projectID) {
		boolean deleted = false ;
		Project project = Project.findById(projectID);
		List<Event> eventList = project.events;
		for (int i = 0; i < eventList.size(); i++) 
		{
			Event event = eventList.get(i);
			for (int j = 0; j < event.notes.size(); j++) 
			{
				int size = event.notes.size();
				event.notes.get(j).delete();
				event.save();
				if(event.notes.size()==size&&size>0)
					deleted = false ;
				else{
					if(event.notes.size()==size&&size==0)
						deleted=true ;
					else{
							if(event.notes.size()==size-1)
								deleted = true ;
							else
							{
								deleted=false;
							}
						}
					}
				}
		}
		return deleted ;
	}
	public static void showNote(long noteId)
	{
		Note note=Note.findById(noteId);
		Project project=null;
		List<Event> allEvents=Event.findAll();
		for(Event event:allEvents)
			if(event.notes.contains(note))
			{
				project=event.project;
				break;
			}
		render(note,project);
	}
}